// 讨论区相关的mutations
export default {
  // 设置讨论列表
  SET_DISCUSSIONS(state, discussions) {
    state.discussions = discussions
  },
  
  // 设置讨论详情
  SET_DISCUSSION_DETAIL(state, discussion) {
    state.discussionDetail = discussion
  },
  
  // 设置回复列表
  SET_REPLIES(state, replies) {
    state.replies = replies
  },
  
  // 设置加载状态
  SET_LOADING(state, loading) {
    state.loading = loading
  },
  
  // 设置保存状态
  SET_SAVING(state, saving) {
    state.saving = saving
  },
  
  // 设置删除状态
  SET_DELETING(state, deleting) {
    state.deleting = deleting
  },
  
  // 设置错误信息
  SET_ERROR(state, error) {
    state.error = error
  },
  
  // 设置当前包名称
  SET_CURRENT_PACKAGE_NAME(state, packageName) {
    state.currentPackageName = packageName
  },
  
  // 设置分页信息
  SET_PAGINATION(state, { page, limit, total }) {
    state.page = page
    state.limit = limit
    state.total = total
  },
  
  // 设置过滤器
  SET_FILTERS(state, filters) {
    state.filters = { ...state.filters, ...filters }
  },
  
  // 添加新讨论
  ADD_DISCUSSION(state, discussion) {
    state.discussions.unshift(discussion)
    state.total += 1
  },
  
  // 更新讨论
  UPDATE_DISCUSSION(state, updatedDiscussion) {
    const index = state.discussions.findIndex(d => d.id === updatedDiscussion.id)
    if (index !== -1) {
      state.discussions.splice(index, 1, updatedDiscussion)
    }
    
    // 如果更新的是当前查看的讨论详情，也更新讨论详情
    if (state.discussionDetail && state.discussionDetail.id === updatedDiscussion.id) {
      state.discussionDetail = updatedDiscussion
    }
  },
  
  // 删除讨论
  DELETE_DISCUSSION(state, discussionId) {
    state.discussions = state.discussions.filter(d => d.id !== discussionId)
    state.total -= 1
    
    // 如果删除的是当前查看的讨论详情，清空讨论详情和回复
    if (state.discussionDetail && state.discussionDetail.id === discussionId) {
      state.discussionDetail = null
      state.replies = []
    }
  },
  
  // 添加新回复
  ADD_REPLY(state, reply) {
    state.replies.push(reply)
    
    // 如果有讨论详情，更新讨论的回复数量
    if (state.discussionDetail) {
      state.discussionDetail.replyCount = (state.discussionDetail.replyCount || 0) + 1
    }
    
    // 如果有讨论列表，也更新对应讨论的回复数量
    const discussionIndex = state.discussions.findIndex(d => d.id === state.discussionDetail?.id)
    if (discussionIndex !== -1) {
      state.discussions[discussionIndex].replyCount = (state.discussions[discussionIndex].replyCount || 0) + 1
    }
  },
  
  // 清空讨论详情和回复
  CLEAR_DISCUSSION_DETAIL(state) {
    state.discussionDetail = null
    state.replies = []
  },
  
  // 清空所有讨论
  CLEAR_ALL_DISCUSSIONS(state) {
    state.discussions = []
    state.discussionDetail = null
    state.replies = []
    state.currentPackageName = ''
    state.page = 1
    state.total = 0
  }
}